package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/images")
public class ImageController {

    /*
    *上传图片
    * 资源:/images
    * 动作:POST
    * 参数:pic 二进制
    **/
    @PostMapping
    public Object upload(@RequestParam MultipartFile pic){
        Map<String,Object> map = new HashMap<>();
        if(pic.getContentType().startsWith("image") && pic.getSize() > 0){
            String imgUrl = UploadUtil.upload(pic);
            map.put("status","1");
            map.put("url",imgUrl);
        }else{
            map.put("status","0");
            map.put("msg","请正确的选中图片");
        }
        return map;
    }
}
